//
//  ZActionSheet.m
//  ArkHealth
//
//  Created by 章翔 on 16/9/6.
//  Copyright © 2016年 xjx. All rights reserved.
//

#import "ZActionSheet.h"
#import "XGConfig.h"
//#import "XGButton.h"

@implementation ZActionSheet{
    void(^_block)(BOOL isCancel);
    CGRect originFrame;
//    XGButton *btnClose;
    BOOL isShowKey;
}

+ (ZActionSheet *)sharedView {
    static ZActionSheet *sharedView;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        sharedView = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        //        sharedView.autoCorner = YES;
    });
    
    return sharedView;
}

+ (void)anctionSheetWithView:(UIView *)view{
    [[self sharedView] anctionSheetWithView:view];
    
}

+ (void)actionsheetWithView:(UIView *)view block:(void (^)(BOOL isCancel))block{
    [[self sharedView] actionSheetWithView:view block:block];
}

+ (void)dismissWithBlock:(void (^)())block{
    [[self sharedView]dismissWithBlock:block];
}

- (void)actionSheetWithView:(UIView *)view block:(void (^)(BOOL isCancel))block{
    _block = block;
    UIView *bgView = [[UIView alloc]init];
    [self addSubview:bgView];
    
    
    UIView *viewContent = view;
    viewContent.frame = CGRectMake(10, 0, KActionSheetWidth, CGRectGetHeight(view.frame));
    viewContent.layer.cornerRadius = 9;
    viewContent.layer.masksToBounds = YES;
    viewContent.backgroundColor = UIColorFromRGB(0xf1f1f0);
    [bgView addSubview:viewContent];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(viewContent.frame) + 7.5, KActionSheetWidth, 56)];
    btn.backgroundColor = UIColorFromRGB(0xf1f1f0);
    btn.layer.cornerRadius = 9;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(onclickCancel:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitleColor:UIColorFromRGB(0x56abe4) forState:UIControlStateNormal];
    [bgView addSubview:btn];
    
    bgView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, CGRectGetMaxY(btn.frame) + 10);
    originFrame = bgView.frame;
    bgView.tag = 1000;
    [self show];
    

    
}

- (void)anctionSheetWithView:(UIView *)view{
    
    
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
    
    [self removeFromSuperview];
    isShowKey = NO;

    CGRect frame = view.frame;
    frame.origin.y = ScreenHeight;
    originFrame = frame;
    view.frame = frame;
    view.tag = 1000;
    [self addSubview:view];
    [self show];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewKeybordHandle:) name:UIKeyboardWillShowNotification object:nil];

    
//    CGFloat btnWidth = 25;
//    CGFloat btnHeight = btnWidth;
//    btnClose = [[XGButton alloc]initWithFrame:CGRectMake(ScreenWidth - btnWidth - 15, 32 + (30 - btnHeight)/2, btnWidth, btnHeight) andCustomStyl:10004];
//    [btnClose setImage:[UIImage imageNamed:@"closeBlue"] forState:UIControlStateNormal];
    
//    [self addSubview:btnClose];
//    [btnClose addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
}


- (void)show{
    // 1.获得最上面的窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    self.frame = window.bounds;
    //显示界面

    
    UIView *view = [self viewWithTag:1000];
    self.alpha = 1;
    self.backgroundColor = [UIColor colorWithWhite:.0 alpha:.5];
    
    if (!isShowKey) {
        [UIView animateWithDuration:.2 animations:^{
            CGRect frame = view.frame;
            frame.origin.y = ScreenHeight - frame.size.height;
            view.frame = frame;
        } completion:^(BOOL finished) {
            
        }];
    }

    
    [self addTarget:self action:@selector(handleTouch) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)dismiss{
    [[ZActionSheet sharedView]dismissWithBlock:nil];
}

- (void)dismissWithBlock:(void(^)())block{
  
    UIView *view = [self viewWithTag:1000];
    self.touchDismiss = NO;
    
    [UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.alpha = 0;
        view.frame = originFrame;
        
    } completion:^(BOOL finished) {

        for (UIView *subView in self.subviews) {
            [subView removeFromSuperview];
        }
        if (block) {
            block();
        }
        [self removeFromSuperview];

    }];
}

- (void)handleTouch{
    
    if (self.touchDismiss) {
        [self dismiss];
    }
}

+ (void)touchDismiss:(BOOL)dismiss{
    [self sharedView].touchDismiss = dismiss;
}

+ (void)backViewClearColor{
    [self sharedView].backgroundColor = [UIColor colorWithWhite:1 alpha:0];
}

+ (void)dismiss{
    [[ZActionSheet sharedView]dismiss];
}

+ (void)show{
    [[ZActionSheet sharedView]show];
}

- (void)onclickCancel:(UIButton *)btn{
    if (_block) {
        [self dismiss];
        _block(YES);
    }
}

- (void)alertViewKeybordHandle:(NSNotification *)notification {
    
    if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
        isShowKey = YES;
        NSDictionary *dic = notification.userInfo;
        NSValue *value = [dic valueForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect kbFrame = value.CGRectValue;
        
        UIView *showingView = [self viewWithTag:1000];
        
        CGRect frame = showingView.frame;
        
        if (kbFrame.size.height > 100 && frame.origin.y + frame.size.height > kbFrame.origin.y) {
            frame.origin.y = kbFrame.origin.y - frame.size.height ;
            [UIView animateWithDuration:0.25 animations:^{
                showingView.frame = frame;
            }];
        }
    }
//    else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]) {
//        UIView *showingView = [self viewWithTag:1000];
//        [UIView animateWithDuration:0.25 animations:^{
//            showingView.y = ScreenHeight - showingView.height;
//        }];
//    }
    
}

@end
